// pages/UserModule/myInfo.js
const app = getApp()
Page({
    data: {
        userInfo: {},
        /*
        userId
        userName
        birthday
        email
        studentId
        accountStatus
        sex
        registrationTime
        accountClass
        accountScore
        lastLoginTime
        signInDays
        avatarUrl
        */
       hasChangeName: 0,
       newName: null,
    },
    onLoad: function (options) {
        var that = this
        that.initUserInfo()
    },
    initUserInfo(){
        var that = this
        wx.request({
            url: `http://${app.globalData.target}/user/getBasicInfo`,
            data: {
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(res){
                console.log(res)
                that.setData({
                    userInfo: res.data.result
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    changeName: function(e){
        this.setData({
            hasChangeName: 1
        })
    },
    bindNameChange: function(e){
        var that = this
        that.setData({
            newName: e.detail.value
        })
    },
    competeChangeName: function(e){
        var that = this
        wx.request({
            url: `http://${app.globalData.target}/user/updateConfig`,
            data: {
                userName: that.data.newName
            },
            header: {
                "hello-token": wx.getStorageSync("hello-token")
            },
            method: 'GET',
            success: function(res){
                console.log(res)
                that.setData({
                    hasChangeName: 0,
                    newName: null,
                    userInfo: {}
                })
                that.initUserInfo()
            },
            fail: function(e){
                console.log(e)
            }
        })
    },
    uploadImage: function(e){
        var that = this
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album','camera'],
            success: function(res){
                const filePath = res.tempFilePaths[0]
                const postData = wx.getFileSystemManager().readFileSync(filePath, "base64")
                wx.uploadFile({
                    url: `http://${app.globalData.target}/user/uploadAvatar`,
                    filePath: filePath,
                    name: 'avatarPic',
                    header: {
                        'content-type': 'multipart/form-data',
                        "hello-token": wx.getStorageSync("hello-token")
                    },
                    success: function(result){
                        const _result = JSON.parse(result.data)
                        console.log(_result)
                        if(_result.code == 0){
                            that.setData({
                                ['userInfo.avatarUrl']: _result.result.avatarUrl
                            })
                            wx.showToast({
                                title: '上传成功',
                                icon: 'none'
                            })
                        }else{
                            wx.showToast({
                                title: '上传失败',
                                icon: 'none'
                            })
                        }
                    },
                    fail: function(e){
                        wx.showToast({
                            title: '上传失败',
                            icon: 'none'
                        })
                    }
                })
            },
            fail: function(e){
                console.log(e)
            }
        })
    }
})